/**
 * 
 */
package co.edu.udem.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import co.edu.udem.dao.PlaceDao;
import co.edu.udem.domain.Place;
import co.edu.udem.service.login.SessionManager;

/**
 * @since 29/07/2012
 */
@Repository("placeDao")
@Transactional
public class PlaceDaoImpl implements PlaceDao {

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/** Instance of session. */
	@Autowired
	private SessionManager sessionManager;

	public List<Place> getObjectList(Date serviceDate, String[][] orderBy,
			Object... params) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("From Place");
		return (List<Place>) query.list();
	}

	public void save(Place place) {
		Session session = sessionFactory.getCurrentSession();
		session.merge(place);
	}

	public List<Place> getPlaceListByCriteria(String subjectDetailCode) {
		Session session = sessionFactory.getCurrentSession();
		Query query;
		if (subjectDetailCode == null) {
			query = session.createQuery("Select place FROM  Place place");
		} else {
			String qString = "Select place FROM  Place place, SubjectDetail det Where det.placeCode = place.placeCode And det.subjectDetailCode = :detailCode";
			query = session.createQuery(qString);
			query.setParameter("detailCode", subjectDetailCode);
		}
		return (List<Place>) query.list();
	}
	
	public Place getPlaceById(String placeCode) {
		Session session = sessionFactory.getCurrentSession();
		Place place = (Place) session.get(Place.class, placeCode);
		return place;
	}
	
	public void delete(String placeCode) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("DELETE FROM Place Where placeCode = :placeCode");
		query.setParameter("placeCode", placeCode);
		query.executeUpdate();
	}

}
